//
//  LGTRWZPersonTopCell.swift
//  KJLGT
//
//  Created by jenkin on 2019/9/2.
//  Copyright © 2019年 jenkin. All rights reserved.
//

import UIKit
enum PersonTopClickType:Int {
    case UserInfoType = 0 //用户信息
    case ChangePublicer = 1 //发布任务
    case ChangeMakeTask = 2 //做任务
    case OrderWaitDeal = 3 //待处理
    case OrderDeal = 4 //处理中
    case OrderSuccess = 5 //成功
    case OrderFail = 6 //失败
    case UserMoney = 7 //用户的钱包
}

class LGTRWZPersonTopCell: UITableViewCell {

    var userImageView:UIImageView?
    var userNameLabel:UILabel?
    var idLabel:UILabel?
    var canuseMoneyLabel:UILabel?
    var rechargeLabel:UILabel?
    var clickBlock:ClickIndexBlock?
    var myOrderView:UIImageView?
    //var myOrderView:UIView?
    
    var _model:Dictionary<String, Any>?
    var model:Dictionary<String, Any>?{
        get{
            return _model
        }set{
            _model = newValue
            self.setData()
        }
    }
    
    func setData(){
        let userModel:UserModel = self.model?["user"] as? UserModel ?? UserModel()
        BaseUtil.baseUtil.setImage(imageView: self.userImageView!, imageUrl: userModel.avatar?.url ?? "")
//        if userModel.user_type == 1{
//            self.myOrderView?.isHidden = true
//            self.myOrderView?.snp.updateConstraints({ (make) in
//                make.height.equalTo(0)
//            })
//        }else{
//            self.myOrderView?.isHidden = false
//            self.myOrderView?.snp.updateConstraints({ (make) in
//                make.height.equalTo(SizeFitFloat(126))
//            })
//        }
        
        self.userNameLabel?.text = userModel.nickname
        let userID:Int = userModel.user_id ?? 0
        self.idLabel?.text = "ID:\(String(describing: userID))"
        let canuseMoney:Double = Double(userModel.amount ?? 0)/100.0
        let rechargeMoney:Double = Double(userModel.recharge ?? 0)/100.0
        self.canuseMoneyLabel?.text = String(format: "%.2f", canuseMoney)
        self.rechargeLabel?.text = String(format: "%.2f", rechargeMoney)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = UITableViewCell.SelectionStyle.none
        
        let topImageView:UIImageView = LGTViewCreateUtil.share.createImageView(image: "pig_bg")
        self.addSubview(topImageView)
        topImageView.snp.makeConstraints { (make) in
            make.left.right.top.equalTo(0)
            make.height.equalTo(SizeFit(136) + IPHONENAV_DIS())
        }
        topImageView.isUserInteractionEnabled = true
        topImageView.tag = PersonTopClickType.UserInfoType.rawValue
        let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(gotoAction))
        topImageView.addGestureRecognizer(tap)
        
        self.userImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(34))
        self.addSubview(self.userImageView!)
        self.userImageView?.backgroundColor = ViewBackColor
        self.userImageView!.snp.makeConstraints { (make) in
            make.top.equalTo(SizeFit(42) + IPHONENAV_DIS())
            make.width.height.equalTo(SizeFit(68))
            make.left.equalTo(SizeFit(21))
        }
        
        self.userNameLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: 0)
        self.userNameLabel?.font = KBFont(size: 16)
        self.addSubview(self.userNameLabel!)
        self.userNameLabel?.snp.makeConstraints({ (make) in
            make.height.equalTo(SizeFit(18))
            make.left.equalTo(SizeFit(104))
            make.top.equalTo(self.userImageView!).offset(SizeFit(14))
        })
        
        self.idLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: SizeFitFloat(12))
        self.addSubview(self.idLabel!)
        self.idLabel?.snp.makeConstraints({ (make) in
            make.height.equalTo(SizeFit(12))
            make.left.equalTo(SizeFit(104))
            make.top.equalTo(self.userImageView!).offset(SizeFit(44))
        })
        
        //xia_zuo
        let gotoBtn:LGTButton = LGTViewCreateUtil.share.createLGTButton(image: "xia_zuo", selected: "xia_zuo", dW: SizeFitFloat(10), dH:  SizeFitFloat(10))
        self.addSubview(gotoBtn)
        gotoBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(self.userImageView!)
            make.width.equalTo(SizeFit(6))
            make.height.equalTo(SizeFit(12))
            make.right.equalTo(SizeFitFloat(-21))
        }
        
        //我的资产
        let myMoneyView:UIImageView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(5))
        myMoneyView.backgroundColor = UIColor.white
        myMoneyView.isUserInteractionEnabled = true
        self.addSubview(myMoneyView)
        myMoneyView.snp.makeConstraints { (make) in
            make.height.equalTo(SizeFitFloat(108))
            make.top.equalTo(topImageView.snp_bottomMargin).offset(SizeFitFloat(-9))
            make.left.equalTo(SizeFitFloat(9))
            make.right.equalTo(SizeFitFloat(-9))
        }
        
        let myMoneyLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: 0, text: "我的资产")
        myMoneyLabel.font = KBFont(size: SizeFitFloat(15))
        myMoneyView.addSubview(myMoneyLabel)
        myMoneyLabel.snp.makeConstraints { (make) in
            make.height.equalTo(SizeFitFloat(16))
            make.left.equalTo(SizeFit(19))
            make.top.equalTo(SizeFitFloat(15))
        }
        
        //我的资产
        let descArray:Array<String> = ["可用余额", "充值金额"]
        self.canuseMoneyLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: 0)
        self.canuseMoneyLabel?.font = KBFont(size: SizeFitFloat(16))
        self.rechargeLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: 0)
        self.rechargeLabel?.font = KBFont(size: SizeFitFloat(16))
        let labelArray:Array<UILabel> = [self.canuseMoneyLabel!, self.rechargeLabel!]
        let itemw:Double = (Double(KScreenW) - SizeFit(18)) / 2.0
        for i in 0..<descArray.count{
            let button:UIButton = UIButton.init()
            myMoneyView.addSubview(button)
            button.snp.makeConstraints { (make) in
                make.left.equalTo(itemw * Double(i))
                make.height.equalTo(SizeFitFloat(40))
                make.top.equalTo(SizeFit(53))
                make.width.equalTo(itemw)
            }
            button.tag = PersonTopClickType.UserMoney.rawValue
            button.addTarget(self, action: #selector(gotoClickAction), for: UIControl.Event.touchUpInside)
            
            let label:UILabel = labelArray[i]
            myMoneyView.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.left.equalTo(itemw * Double(i))
                make.height.equalTo(SizeFitFloat(18))
                make.top.equalTo(SizeFit(53))
                make.width.equalTo(itemw)
            }
            
            let desc:String = descArray[i] as String
            let descLabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: GrayColor, fontSize: SizeFitFloat(12), text: desc)
            myMoneyView.addSubview(descLabel)
            descLabel.snp.makeConstraints { (make) in
                make.top.equalTo(SizeFit(79))
                make.height.equalTo(SizeFit(12))
                make.centerX.equalTo(label)
                make.width.equalTo(itemw)
            }
        }
        
        self.myOrderView = LGTViewCreateUtil.share.createImageViewCorner(corner: SizeFitFloat(5), imageName: "")
        //self.myOrderView = UIView.init()
        //self.myOrderView?.backgroundColor = UIColor.orange
        self.myOrderView?.backgroundColor = UIColor.white
        self.myOrderView?.isUserInteractionEnabled = true
        self.addSubview(self.myOrderView!)
        
        let userModel:UserModel = UserModel.share ?? UserModel()
        if userModel.user_type == 2{
            self.myOrderView?.snp.makeConstraints({ (make) in
                make.height.equalTo(SizeFitFloat(117))
                make.top.equalTo(topImageView.snp_bottomMargin).offset(SizeFit(108))
                make.left.equalTo(SizeFitFloat(9))
                make.right.equalTo(SizeFitFloat(-9))
            })
        }else{
            self.myOrderView?.snp.makeConstraints({ (make) in
                make.height.equalTo(SizeFitFloat(0))
                make.top.equalTo(topImageView.snp_bottomMargin).offset(SizeFit(99))
                make.left.equalTo(SizeFitFloat(9))
                make.right.equalTo(SizeFitFloat(-9))
            })
        }
        
        //我的订单
        let myOrderLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: 0, text: "我的订单 ")
        myOrderLabel.font = KBFont(size: SizeFitFloat(15))
        self.myOrderView?.addSubview(myOrderLabel)
        myOrderLabel.snp.makeConstraints { (make) in
            make.height.equalTo(SizeFitFloat(16))
            make.left.equalTo(SizeFit(19))
            make.top.equalTo(SizeFitFloat(15))
        }

        let orderArray:Array<Dictionary<String, Any>> = [["title":"待处理", "icon":"tobeprocessed", "tag":PersonTopClickType.OrderWaitDeal.rawValue],
                                                         ["title":"处理中", "icon":"processing", "tag":PersonTopClickType.OrderDeal.rawValue],
                                                         ["title":"成功", "icon":"success2", "tag":PersonTopClickType.OrderSuccess.rawValue],
                                                         ["title":"失败", "icon":"fail", "tag":PersonTopClickType.OrderFail.rawValue]]
        let count:Int = orderArray.count
        let itemW:CGFloat = (KScreenW - SizeFitFloat(18)) / CGFloat(count)
        let itemH = SizeFit(46)
        for i in 0..<count{
            let info:Dictionary<String, Any> = orderArray[i]
            let icon:String = info["icon"] as! String
            let title:String = info["title"] as! String
            let tag:Int = info["tag"] as! Int

            let button:UIButton = UIButton.init()
            self.myOrderView?.addSubview(button)
            button.snp.makeConstraints { (make) in
                make.left.equalTo(CGFloat(i) * itemW)
                make.width.equalTo(itemW)
                make.height.equalTo(itemH)
                make.top.equalTo(SizeFitFloat(54))
            }

            let iconView:UIImageView = LGTViewCreateUtil.share.createImageView(image: icon)
            button.addSubview(iconView)
            iconView.snp.makeConstraints { (make) in
                make.centerX.equalTo(button)
                make.top.equalTo(0)
                make.width.height.equalTo(SizeFitFloat(21))
            }

            let label:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.center, textColor: TextColor, fontSize: SizeFitFloat(12), text: title)
            button.addSubview(label)
            label.snp.makeConstraints { (make) in
                make.top.equalTo(SizeFitFloat(34))
                make.centerX.equalTo(button)
                make.height.equalTo(SizeFitFloat(12))
            }

            button.tag = tag
            button.addTarget(self, action: #selector(gotoClickAction), for: UIControl.Event.touchUpInside)
        }
        
        self.contentView.backgroundColor = ViewBackColor
        let postArray:Array<Dictionary<String, Any>> = [
            ["title":"我要去发布任务", "desc":"助你一臂之力", "icon":"imreleasingit"],
            ["title":"我要接单做任务", "desc":"玩赚到手软", "icon":"tmission"]
        ]
        let buttonW:Double = itemw - SizeFit(4.5)
        let backTop = SizeFit(9)
        for i in 0..<postArray.count{
            let info:Dictionary<String, Any> = postArray[i]
            let title:String = info["title"] as! String
            let desc:String = info["desc"] as! String
            let icon:String = info["icon"] as! String
            
            let backBtn:LGTButton = LGTViewCreateUtil.share.createTextLGTButton(title: "", font: Font_Normal_12, color: UIColor.white, selectedColor: UIColor.white, corner: SizeFitFloat(5), backColor: UIColor.white)
            backBtn.tag = i + 1
            backBtn.addTarget(self, action: #selector(gotoClickAction), for: UIControl.Event.touchUpInside)
            self.addSubview(backBtn)
            let leftX = SizeFit(9) + (buttonW + SizeFit(9)) * Double(i)
            backBtn.snp.makeConstraints { (make) in
                make.left.equalTo(leftX)
                make.height.equalTo(SizeFitFloat(63))
                make.width.equalTo(buttonW)
                //make.top.equalTo(self.myOrderView!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
                if #available(iOS 11.0, *){
                   make.top.equalTo(self.myOrderView!.safeAreaLayoutGuide.snp.bottom).offset(backTop)
                }else{
                    make.top.equalTo(self.myOrderView!.snp_bottomMargin).offset(backTop)
                }
                make.bottom.equalTo(-backTop)
            }
            
            let titleLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: TextColor, fontSize: 0, text: title)
            titleLabel.font = KBFont(size: SizeFitFloat(14))
            backBtn.addSubview(titleLabel)
            titleLabel.snp.makeConstraints { (make) in
                make.left.equalTo(SizeFit(24))
                make.top.equalTo(SizeFit(13))
                make.height.equalTo(SizeFitFloat(15))
            }
            
            let descLabel:UILabel = LGTViewCreateUtil.share.createLabel(textAlignment: NSTextAlignment.left, textColor: GrayColor, fontSize: SizeFitFloat(10), text: desc)
            backBtn.addSubview(descLabel)
            descLabel.snp.makeConstraints { (make) in
                make.left.equalTo(titleLabel)
                make.top.equalTo(SizeFit(36))
                make.height.equalTo(SizeFit(11))
            }
            
            let iconImage:UIImageView = LGTViewCreateUtil.share.createImageView(image: icon)
            backBtn.addSubview(iconImage)
            iconImage.snp.makeConstraints { (make) in
                make.width.height.equalTo(SizeFit(17))
                make.top.equalTo(SizeFit(37))
                make.right.equalTo(-SizeFit(17))
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc func gotoAction(tap:UITapGestureRecognizer){
        self.gotoClickAction(sender: tap.view!)
    }
    
    @objc func gotoClickAction(sender:UIView){
        
        if self.clickBlock != nil{
            self.clickBlock!(sender.tag)
        }
    }
}
